<?php


namespace app\api\controller\mjlbb\goods;
use app\common\controller\Api;
use think\Db;

class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @获取商品列表
     */
    public function GetGoodsList()
    {
        $list = Db::table('fa_shopro_goods')
            ->where('status','up')
            ->where('category_type', input('category_type'))
            ->order('weigh', 'desc')
            ->select();

        if(!$list){
            $this->error('商品资源为空');
        }
        $this->success('获取商品资源', $list);
    }

    /**
     * @获取商品详情
     */
    public function GetGoodDetail()
    {
        $info = Db::table('fa_shopro_goods')
            ->alias('g')
            ->where('g.status','up')
            ->where('g.id', input('goods_id'))
            ->select();
        // 商品轮播图
        foreach ($info as $k => $v)
        {
            $imgList = explode(',', $v['images']);
            $list = array();
            foreach ($imgList as $ik => $iv)
            {
                array_push($list, config('mjlbb')['host'] . $iv);
            }
            $info[$k]['images'] = $list;  // 商品轮播图
            $info[$k]['image'] = config('mjlbb')['host'] . $v['image'];//商品主图
            $info[$k]['params'] = json_decode($v['params']);
        }

        $sku = Db::table('fa_shopro_goods_sku')
            ->where('goods_id', input('goods_id'))
            ->select();
        $parent = [];
        foreach ($sku as $skk => $skv)
        {
            if($skv['pid'] == 0){
                $pid = ['id'=>$skv['id'], 'name'=>$skv['name'], 'son'=>[]];
                array_push($parent, $pid);
            }
        }
        foreach ($parent as $skks => $skvs)
        {
            foreach ($sku as $kk => $vv)
            {
                if($skvs['id'] == $vv['pid']){
                    $spid = ['sid'=>$vv['id'], 'sname'=>$vv['name'], 'select'=>0];
                    array_push($parent[$skks]['son'], $spid);
                }
            }
        }
        $info[0]['sku'] = $parent;

        if(!$info){
            $this->error('商品资源为空');
        }
        $this->success('获取商品资源', $info);
    }

    /**
     * @加入购物车接口
     */
    public function AddShopCar()
    {
       $data = [
           'id'             => input('id'),
           'user_id'        => input('user_id'),
           'goods_id'       => input('goods_id'),
           'goods_num'      => input('goods_num'),      // 总数
           'sku_price_id'   => input('sku_price_id'),   // 规格id
           'createtime'     => time()
       ];
        $info = Db::table('fa_shopro_cart')
            ->insert($data, true);
       if(!$info)
       {
           $this->error('加入失败');
       }
       $this->success('更新成功', $info);
    }

    /**
     * @获取购车列表
     */
    public function GetShopCarList()
    {
        $list = Db::table('fa_shopro_cart')
            ->where('user_id','user_id')
            ->order('createtime', 'desc')
            ->select();
    }
}
